package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private RoomLabelService roomLabelService;
    @Autowired
    private RoomAttrValueService roomAttrValueService;
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private RoomLabelMapper roomLabelMapper;
    @Autowired
    private RoomAttrValueMapper roomAttrValueMapper;
    @Autowired
    private RoomPaymentTypeMapper roomPaymentTypeMapper;
    @Autowired
    private RoomLeaseTermMapper roomLeaseTermMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private RoomFacilityMapper roomFacilityMapper;

    @Override
    public void saveOrUpdateRoomInfo(RoomSubmitVo roomSubmitVo) {
        //判断更新或新增
        boolean isUpdate = roomSubmitVo.getId() != null && roomSubmitVo.getId() != 0;
        //保存或更新房间信息
        this.saveOrUpdate(roomSubmitVo);
        //获取房间ID
        Long id = roomSubmitVo.getId();
        //修改功能
        if (!isUpdate) {
            //删除房间图片信息
            LambdaQueryWrapper<GraphInfo> queryWrapperGraphInfo = new LambdaQueryWrapper<>();
            queryWrapperGraphInfo.eq(GraphInfo::getItemType, ItemType.ROOM);
            queryWrapperGraphInfo.eq(GraphInfo::getItemId, id);
            graphInfoService.remove(queryWrapperGraphInfo);
            //删除房间配套信息
            LambdaQueryWrapper<RoomFacility> queryWrapperRoomFacility = new LambdaQueryWrapper<>();
            queryWrapperRoomFacility.eq(RoomFacility::getRoomId, id);
            roomFacilityService.remove(queryWrapperRoomFacility);
            //删除房间标签信息
            LambdaQueryWrapper<RoomLabel> queryWrapperRoomLabel = new LambdaQueryWrapper<>();
            queryWrapperRoomLabel.eq(RoomLabel::getRoomId, id);
            roomLabelService.remove(queryWrapperRoomLabel);
            //删除房间属性信息
            LambdaQueryWrapper<RoomAttrValue> queryWrapperRoomAttrValue = new LambdaQueryWrapper<>();
            queryWrapperRoomAttrValue.eq(RoomAttrValue::getRoomId, id);
            roomAttrValueService.remove(queryWrapperRoomAttrValue);
            //删除房间支付方式
            LambdaQueryWrapper<RoomPaymentType> queryWrapperRoomPaymentType = new LambdaQueryWrapper<>();
            queryWrapperRoomPaymentType.eq(RoomPaymentType::getRoomId, id);
            roomPaymentTypeService.remove(queryWrapperRoomPaymentType);
            //删除房间租期信息
            LambdaQueryWrapper<RoomLeaseTerm> queryWrapperRoomLeaseTerm = new LambdaQueryWrapper<>();
            queryWrapperRoomLeaseTerm.eq(RoomLeaseTerm::getRoomId, id);
            roomLeaseTermService.remove(queryWrapperRoomLeaseTerm);
        }
        //新增图片信息
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        List<GraphInfo> graphInfoList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(graphVoList)) {
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                BeanUtils.copyProperties(graphVo, graphInfo);
                graphInfo.setItemId(id);
                graphInfo.setItemType(ItemType.ROOM);
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setName(graphVo.getName());
                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);
        }
        //新增属性信息
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        List<RoomAttrValue> roomAttrValueList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(attrValueIds)) {
            for (Long attrValueId : attrValueIds) {
                RoomAttrValue roomAttrValue = RoomAttrValue.builder().attrValueId(attrValueId).roomId(id).build();
                roomAttrValueList.add(roomAttrValue);
            }
            roomAttrValueService.saveBatch(roomAttrValueList);
        }
        //新增支付方式
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        List<RoomPaymentType> roomPaymentTypeList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(paymentTypeIds)) {
            for (Long paymentTypeId : paymentTypeIds) {
                RoomPaymentType roomPaymentType = RoomPaymentType.builder().paymentTypeId(paymentTypeId).roomId(id).build();
                roomPaymentTypeList.add(roomPaymentType);
            }
            roomPaymentTypeService.saveBatch(roomPaymentTypeList);
        }
        //新增租期方式
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        List<RoomLeaseTerm> roomLeaseTermList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(leaseTermIds)) {
            for (Long leaseTermId : leaseTermIds) {
                RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder().leaseTermId(leaseTermId).roomId(id).build();
                roomLeaseTermList.add(roomLeaseTerm);
            }
            roomLeaseTermService.saveBatch(roomLeaseTermList);
        }
        //新增配套信息
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        List<RoomFacility> roomFacilityList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(facilityInfoIds)) {
            for (Long facilityInfoId : facilityInfoIds) {
                RoomFacility roomFacility = RoomFacility.builder().facilityId(facilityInfoId).roomId(id).build();
                roomFacilityList.add(roomFacility);
            }
            roomFacilityService.saveBatch(roomFacilityList);
        }
        //新增标签信息
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        List<RoomLabel> roomLabelList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(labelInfoIds)) {
            for (Long labelInfoId : labelInfoIds) {
                RoomLabel roomLabel = RoomLabel.builder().labelId(labelInfoId).roomId(id).build();
                roomLabelList.add(roomLabel);
            }
            roomLabelService.saveBatch(roomLabelList);
        }
    }

    @Override
    public IPage<RoomItemVo> findRoomItemVoPage(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.findRoomItemVoPage(page, queryVo);
    }

    @Override
    public RoomDetailVo findRoomDatailById(Long id) {
        //查询房间信息
        RoomInfo roomInfo = this.getById(id);
        //获取房间对应的图片信息
        List<GraphVo> graphVoList = graphInfoMapper.findGraphVoListByItemTypeAndItemId(ItemType.ROOM,id);
        //获取房间对应的配套信息
        List<FacilityInfo> roomFacilitieList = roomFacilityMapper.findRoomFacilitieListById(id);
        //获取房间对应的标签信息
        List<LabelInfo> roomLabelList = roomLabelMapper.findRoomLabelListById(id);
        //获取房间对应的支付方式
        List<PaymentType> roomPaymentTypeList = roomPaymentTypeMapper.findRoomPaymentTypeListById(id);
        //获取房间对应的属性信息
        List<AttrValueVo> roomAttrValueList = roomAttrValueMapper.findRoomAttrValueList(id);
        //获取房间对应的租期信息
        List<LeaseTerm> roomLeaseTermList = roomLeaseTermMapper.findRoomLeaseTermList(id);
        //创建RoomDetailVo对象
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        //复制
        BeanUtils.copyProperties(roomInfo, roomDetailVo);
        //赋值
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setFacilityInfoList(roomFacilitieList);
        roomDetailVo.setLabelInfoList(roomLabelList);
        roomDetailVo.setPaymentTypeList(roomPaymentTypeList);
        roomDetailVo.setAttrValueVoList(roomAttrValueList);
        roomDetailVo.setLeaseTermList(roomLeaseTermList);
        return roomDetailVo;
    }

    @Override
    public void removeRoomById(Long id) {
        //删除房间信息
        this.removeById(id);
        //删除房间所对应的配套信息
        LambdaQueryWrapper<RoomFacility> facilityInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        facilityInfoLambdaQueryWrapper.eq(RoomFacility::getRoomId, id);
        roomFacilityService.remove(facilityInfoLambdaQueryWrapper);
        //删除房间所对应的标签信息
        LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId, id);
        roomLabelService.remove(roomLabelLambdaQueryWrapper);
        //删除房间所对应的支付方式
        LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId, id);
        roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);
        //删除房间所对应的属性信息
        LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId, id);
        roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);
        //删除房间所对应的租期信息
        LambdaQueryWrapper<RoomLeaseTerm> leaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
        leaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId, id);
        roomLeaseTermService.remove(leaseTermLambdaQueryWrapper);
        //删除房间所对应的图片信息
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM).eq(GraphInfo::getItemId,id);
        graphInfoService.remove(graphInfoLambdaQueryWrapper);
    }
}




